package zestaw1.zmienna_ilosc_parametrow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Autor: Daniel Michalski
 * Data: 12.07.13
 */

public class Varargs extends JFrame {
    private JTextArea report = new JTextArea(25, 40);

    public Varargs() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        report.setBorder(BorderFactory.createTitledBorder("Raport"));
        add(new JScrollPane(report));

        JPanel p = new JPanel();

        final JLabel lab = new JLabel("Label");
        lab.setOpaque(true);
        p.add(lab);

        final JButton b1 = new JButton("Kolory zestaw1");
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setColor(Color.RED, b1);
            }
        });
        p.add(b1);

        final JButton b2 = new JButton("Kolory 2");
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setColor(Color.GREEN, b2, lab);
            }
        });
        p.add(b2);

        final JButton b3 = new JButton("Kolory 3");
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setColor(Color.YELLOW, b1, b2, b3, lab);
            }
        });
        p.add(b3);

        add(p, BorderLayout.NORTH);

        pack();
        setVisible(true);

    }

    private void setColor(final Color color, final Component ... comps) {
        report.append("Liczba przekazanych komponentów: " + comps.length + '\n');
        report.append("Szerokość pierwszego komponentu: " + comps[0].getWidth() + '\n');
        report.append("Szerokość ostatniego komponentu: " + comps[comps.length-1].getWidth() + '\n');
        for(Component c : comps)
            c.setBackground(color);
        met(1, 3, 5);


    }

    private void met(int ... p) {
        for (int v : p)
            report.append(v + " ");
    }

    public static void main(String[] args) {
        new Varargs();
    }
}
